//
//  HomeView.swift
//  SwiftUi
//
//  Created by xm on 02/09/2025.
//

import SwiftUI

struct CNHomeView: View {
    
    @StateObject var userData: UserData = UserData()

    var categories: [String: [CNLandmark]] {
        Dictionary(grouping: userData.landmarks, by: {$0.category} )
    }
    
    var landmarks:[CNLandmark] {
        self.userData.landmarks
    }
    
    var featuredImageNames:[String] {
        self.userData.landmarks.filter {
            $0.isFeatured
        }.map {
            $0.imageName
        }
    }
    
    @State var showingProfile = false
    
    var profileButton: some View {
        Button(action: { self.showingProfile.toggle() }) {
            Image(systemName: "person.crop.circle")
                .imageScale(.large)
                .accessibility(label: Text("User Profile"))
                .padding()
        }
    }
    
    var body: some View {
        NavigationStack{
            List{
//                landmarks[0].image
//                    .resizable()
//                    .scaledToFill()
//                    .frame(height: 200)
//                    .clipped()
//                    .listRowInsets(EdgeInsets())
                BasicCarousel(images: featuredImageNames)
                    .frame(height: 200)
                    .padding(-20)
                
                ForEach(categories.keys.sorted(), id: \.self) { categoryName in
                    CategoryCell(categories: categories[categoryName], categoryName: categoryName)
                    
                }.listRowInsets(EdgeInsets())
                
                
                NavigationLink(destination: CNLandmarkList()) {
                    Text("产看所有地标")
                }
                
            }
            .navigationTitle(Text("精选"))
            .navigationBarItems(trailing:profileButton)
            .sheet(isPresented: $showingProfile) {
                CNProfile()
            }
            .listStyle(.plain)
            
            
            
        }
        .environmentObject(userData)
        
        
        
    }
}


#Preview {
    
        CNHomeView()
}

//#Preview {
//    
//        CNHomeView().environment(\.colorScheme, .dark)
//    
//}
//#Preview {
//  
//        CNHomeView().environment(\.sizeCategory, .accessibilityLarge)
//    
//}
